﻿namespace Sidvall.WorkItemManager.UI.Entities
{
	public sealed partial class WorkItemCollection
	{
		#region Business Members

		#region MinimumLastExecutionItem

		public WorkItem MinimumLastExecutionItem
		{
			get
			{
				WorkItem workItem;

				workItem = null;
				foreach (var item in this)
				{
					if (item.LastExecutionEndTime != null)
					{
						if (workItem == null)
							workItem = item;
						else if (workItem.LastExecutionEndTime > item.LastExecutionEndTime)
							workItem = item;
					}
				}
				return workItem;
			}
		}

		#endregion

		#region Add

		public void Add(WorkItem item, int maxCount)
		{
			Add(item);
			if (this.Count > maxCount)
			{
				item = this.MinimumLastExecutionItem;
				Remove(item);
			}
		}

		#endregion

		#endregion
	}
}